﻿using System;
using System.Reflection;
using System.Collections;

namespace ConnectLib.DAO
{
    [Serializable]
    public class ProductDao
    {
        private int _id;
        private int _invId;
        private string _name;
        private int _quality;
        private string _dimension;
        private float _weight;
        private string _note;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public int InvId
        {
            get { return _invId; }
            set { _invId = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Quality
        {
            get { return _quality; }
            set { _quality = value; }
        }

        public string Dimension
        {
            get { return _dimension; }
            set { _dimension = value; }
        }

        public float Weight
        {
            get { return _weight; }
            set { _weight = value; }
        }

        public string Note
        {
            get { return _note; }
            set { _note = value; }
        }

        public void InitEmptyValue()
        {
            _id = 0;
            _invId = 0;
            _name = string.Empty;
            _quality = 0;
            _dimension = string.Empty;
            _weight = 0;
            _note = string.Empty;
        }

        public ProductDao()
        {
            InitEmptyValue();
        }

        public ProductDao(Hashtable hsObj)
        {
            InitEmptyValue();
            try
            {
                PropertyInfo[] arrProInfo = this.GetType().GetProperties();
                PropertyInfo proInfo;
                object objValue;
                string sValue;
                string proName;
                for (int i = 0; i < arrProInfo.Length; i++)
                {
                    proInfo = arrProInfo[i];
                    proName = proInfo.Name.ToLower();
                    if (hsObj.ContainsKey(proName))
                    {
                        objValue = hsObj[proName];
                        if (proInfo.PropertyType.Name.ToLower() == "byte[]")
                        {
                            if (objValue != null && objValue != DBNull.Value)
                            {
                                proInfo.SetValue(this, (byte[]) objValue, null);
                            }
                        }
                        else
                        {
                            sValue = Convert.ToString(objValue);
                            if (string.IsNullOrEmpty(sValue))
                                continue;
                            switch (proInfo.PropertyType.Name.ToLower())
                            {
                                case "byte":
                                    proInfo.SetValue(this, Convert.ToByte(objValue), null);
                                    break;
                                case "int64":
                                    proInfo.SetValue(this, Convert.ToInt64(objValue), null);
                                    break;
                                case "int32":
                                    proInfo.SetValue(this, Convert.ToInt32(objValue), null);
                                    break;
                                case "datetime":
                                    proInfo.SetValue(this, Convert.ToDateTime(objValue), null);
                                    break;
                                case "byte[]":
                                    break;
                                default:
                                    proInfo.SetValue(this, objValue, null);
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

